/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DataBase;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;


public class DataBase {


    public static Connection cn;
    private final static String DRIVER = "org.apache.derby.jdbc.ClientDriver";
    private final static String CONEXAO = "jdbc:derby://localhost:1527/BancoSisam;user=APP;password=app";

    public static void ConectarDataBase() {
        try {
            Class.forName(DRIVER);
            cn = DriverManager.getConnection(CONEXAO);
        } catch (SQLException ex) {
            Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void desconectarBancoDados() {
        try {
            cn.close();
        } catch (SQLException ex) {
            Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void executaOperacao(String SQL, Object[] valores) {
        try {
            PreparedStatement st;
            st = DataBase.cn.prepareStatement(SQL);
            for (int i = 0; i < valores.length; i++) {
                st.setObject(i + 1, valores[i]);
            }
            st.executeUpdate();
            st.close();
        } catch (SQLException ex) {
            Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public static Resultado executaConsulta(String SQL, Object[] valores) {

        try {
            PreparedStatement st;
            st = DataBase.cn.prepareStatement(SQL);
            for (int i = 0; i < valores.length; i++) {
                st.setObject(i + 1, valores[i]);
            }

            ResultSet rs = st.executeQuery();
            Resultado resultado = new Resultado(rs);

            return resultado;

        } catch (SQLException ex) {
            Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }
}
